const ProviderDao = require("../../dao/provider/provider_dao.js");

const ProviderService = {
	add(req, res, next) {
		const {
			prov_code,
			prov_name,
			linkman,
			telephone,
			fax
		} = req.body;
		//获取当前时间
		const create_time = new Date().toLocaleDateString();
		ProviderDao.add({
				prov_code,
				prov_name,
				linkman,
				telephone,
				fax,
				create_time
			})
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						message: "添加成功",
						data: data
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: err,
					res_body: {
						status: 0,
						message: "添加失败"
					}
				});
			})
	},
	//根据商品编码查询
	findByCode(req, res, next) {
		const {
			prov_code
		} = req.query;
		ProviderDao.findByCode(prov_code)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						data: data
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: err,
					res_body: {
						status: 0
					}
				});
			})
	},
	findByPage(req, res, next) {
		const {
			page
		} = req.body;

		ProviderDao.findByPage(page)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						data: data
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: err,
					res_body: {
						status: 0,
						message: "查询页码数据失败"
					}
				});
			})
	},
	//根据供应商名称查询数据
	findByProv_name(req, res, next) {
		const {
			prov_name
		} = req.query;

		ProviderDao.findByProv_name(prov_name)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						data: data
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: err,
					res_body: {
						status: 0,
						message: "根据供应商名称查询数据失败"
					}
				});
			})
	},
	//修改供应商
	modify(req, res, next) {
		const {
			_id,
			prov_code,
			prov_name,
			linkman,
			telephone,
			fax
		} = req.body;

		console.log(_id)
		const create_time = new Date().toLocaleDateString();

		ProviderDao.modify(_id, {
								prov_code,
								prov_name,
								linkman,
								telephone,
								fax,
								create_time
			})
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						data: data,
						message: "修改成功"
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: err,
					res_body: {
						status: 0,
						message: "修改失败"
					}
				});
			})
	},
	//删除供应商
	delete(req, res, next) {
		const {
			_id,
		} = req.body;

		ProviderDao.delete({
				_id
			})
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						data: data,
						message: "删除成功"
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: err,
					res_body: {
						status: 0,
						message: "删除失败"
					}
				});
			})
	}
}

module.exports = ProviderService;